/**
 * Created by wangcan on 17/10/31.
 */
'use strict';
angular.module('backofficeApp.ambCommissionDayManager.controller', [])
    .controller('ambCommissionDayCtrl', ['$scope','$http','$routeParams','$location', function ($scope, $http,$routeParams,$location) {

        $scope.currentPage = 1;
        $scope.pageSize = 15;
        $scope.startdate = $routeParams.orderDate;
        $(".alert").css("display","block");
        // 当前系统时间
        $scope.currentSysDate = new Date().format("yyyy-MM-dd");

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/commission/ambCommisionDayList/:page/:size',
            ajax: $http,
            success:function(data){
                if (data.data.code != 0) {
                    if (data.data.code == 1) {
                        alert("用户在该日期中不是大使！");
                    } else {
                        alert("所选日期中没有大使用户！");
                    }
                }
                $(".alert").css("display","none");
                if(data && data.data) {
                    $("#startDateSelect").val($scope.startdate);
                    $scope.ambCommDayList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;
                    $scope.currentPage = $(".am-active").text();
                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $(".alert").css("display","none");
                //$scope.communityList = null;
            },
            params: {startdate: $scope.startdate}
        });

        //搜索
        $scope.search = function (current) {
            if (current) {
                $scope.currentPage = $scope.pagination.currentPage;
            }else {
                $scope.pagination.currentPage = 1;
                $scope.currentPage = 1;
            }

            var startDateSelect = $("#startDateSelect").val();
            if(!startDateSelect) {
                alert('下单日期不能为空!');
                return;
            }
            if (startDateSelect > $scope.currentSysDate) {
                alert('下单日期不能大于当前系统时间!');
                return;
            }
            $(".alert").css("display","block");
            var option = $scope.option;
            var mobilePhone = null;
            var nickName = null;
            if (option && option.phone) mobilePhone = option.phone;
            if (option && option.nickName) nickName = option.nickName;
            var opt = {
                startdate:startDateSelect,
                mobilePhone:mobilePhone,
                nickName:nickName
            };
            $http.post('http://' + backOfficeHost + '/rest/v1.0/commission/ambCommisionDayList/' + $scope.currentPage  + '/' + $scope.pageSize, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if (data.data.code != 0) {
                    if (data.data.code == 1) {
                        alert("用户在该日期中不是大使！");
                    } else {
                        alert("所选日期中没有大使用户！");
                    }
                }
                $(".alert").css("display","none");
                $scope.ambCommDayList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = opt;
                $('#loading').modal("close");
            }).error(function (err) {
                $(".alert").css("display","none");
                $('#loading').modal("close");
                alert(err);
            });
        }

        // 导出
        $scope.exportdata = function (option, target) {
            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;
            var startDateSelect = $("#startDateSelect").val();
            if(!startDateSelect){
                alert('下单日期不能为空!');
                return;
            }
            if (startDateSelect > $scope.currentSysDate) {
                alert('下单日期不能大于当前系统时间!');
                return;
            }
            $(".alert").css("display","block");
            var option = $scope.option;
            var href = 'http://' + backOfficeHost + '/rest/v1.0/commission/ambCommisionDayDataExport/?isValid=true';
            if (startDateSelect)  href += "&startdate=" + startDateSelect;
            if (option && option.phone) href += "&mobilePhone=" + option.phone;
            if (option && option.nickName) href += "&nickName=" + option.nickName;
            location.href = href;
            $(".alert").css("display","none");
        }

        // 跳转到大使用金页面
        // $scope.toAmbCommission = function (orderDate) {
        //     alert("跳转到大使用金页面。。。" + orderDate);
        // }
    }])